package com.bwie.house.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.house.dao.AddressDao;
import com.bwie.house.entity.AddressEntity;
import com.bwie.house.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bwie.house.entity.SubwayEntity;
import com.bwie.house.service.SubwayService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 *
 *
 * @author search_house
 * @date 2022-04-21 20:35:04
 */
@RestController
@RequestMapping("house/subway")
public class SubwayController {
    @Autowired
    private SubwayService subwayService;

    @Autowired
    private AddressDao addressDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = subwayService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据所选ID  进行回显
    * @author 張泽宇
    * @date 2022/4/23 11:16
    * @return
    */
    @RequestMapping("/info/{subwayId}")
    public R info(@PathVariable("subwayId") Integer subwayId){
		SubwayEntity subway = subwayService.getById(subwayId);

        return R.ok().put("subway", subway);
    }

    /**
    * @author 張泽宇
     * 查询 省
    * @date 2022/4/22 14:58
    * @return
    */
    @GetMapping("/sheng/{parentId}")
    public R shengList(@PathVariable("parentId") Integer parentId){
        QueryWrapper<AddressEntity> w = new QueryWrapper<>();
        List<AddressEntity> address = addressDao.selectList(w.eq("parent_id", parentId));

        return R.ok().put("address", address);
    }

    /**
     * 获取市
    * @author 張泽宇
    * @date 2022/4/22 14:58
    * @return
    */
    @GetMapping("/shi/{parentId}")
    public R shiList(@PathVariable("parentId") Integer parentId){
        QueryWrapper<AddressEntity> w = new QueryWrapper<>();
        List<AddressEntity> address = addressDao.selectList(w.eq("parent_id", parentId));
        return R.ok().put("address", address);
    }

    /**
     * 获取县
    * @author 張泽宇
    * @date 2022/4/23 11:15
    * @return
    */
    @GetMapping("/xian/{parentId}")
    public R xianList(@PathVariable("parentId") Integer parentId){
        QueryWrapper<AddressEntity> w = new QueryWrapper<>();
        List<AddressEntity> address = addressDao.selectList(w.eq("parent_id", parentId));
        return R.ok().put("address", address);
    }

    /**
    * @author 張泽宇
     *  地铁状态修改 updateStatus
    * @date 2022/4/22 18:44
    * @return
    */
    @GetMapping("/updateStatus/{status}/{subwayId}")
    public R updateStatus(@PathVariable("status") Integer status,@PathVariable("subwayId") Integer subwayId){
        //  前台穿过来的地铁ID   进行查找数据
        SubwayEntity subway = subwayService.getById(subwayId);
        // 如果对象不为空
        if(status !=null){
            //进行修改状态
            subway.setStatus(status);
            subwayService.updateById(subway);
            //修改状态成功
            return R.ok();
        }
        //失败  ID没有相对应的数据
        return R.error();
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SubwayEntity subway){
		subwayService.save(subway);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SubwayEntity subway){
		subwayService.updateById(subway);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] subwayIds){
		subwayService.removeByIds(Arrays.asList(subwayIds));

        return R.ok();
    }

}
